package com.breezelin.freshmarket.network.apis;

/**
 * Created by Breeze Lin
 * 13:01 2016/5/2
 * 1linyufeng1@gmail.com
 */

import com.breezelin.freshmarket.entities.EmptyEntity;
import com.breezelin.freshmarket.entities.data.ProductDetail;
import com.breezelin.freshmarket.entities.req.AboutReq;
import com.breezelin.freshmarket.entities.req.ProductReq;
import com.breezelin.freshmarket.entities.req.QueryReq;
import com.breezelin.freshmarket.entities.rsp.CategoriesRsp;
import com.breezelin.freshmarket.entities.rsp.HomeRsp;
import com.breezelin.freshmarket.entities.JsonStruct;
import com.breezelin.freshmarket.entities.network.ReqHeader;
import com.breezelin.freshmarket.entities.network.ReqNRsp;
import com.breezelin.freshmarket.entities.rsp.HotWordRsp;
import com.breezelin.freshmarket.entities.rsp.QueryRsp;
import com.breezelin.freshmarket.network.ApiResolver;

import org.json.JSONObject;

import java.util.List;

/**
 * 内容相关请求
 */
public final class ContentApi {
	private ContentApi() {
		// 私有构造
	}

	/**
	 * 主页内容获取
	 */
	public static final String HOME_CONTENTS = "yiguo.mapi.v3.app.home.get";
	/**
	 * 所有类别
	 */
	public static final String ALL_CATEGORY = "yiguo.mapi.v3.category.all.get";
	/**
	 * 查询
	 */
	public static final String QUERY = "yiguo.mapi.v3.commodity.list.get";
	/**
	 * K
	 * 获取产品详情
	 */
	public static final String GET_PRODUCT_DETAIL = "yiguo.mapi.v3.commodity.get";
	/**
	 * 获取相关产品
	 */
	public static final String GET_RELATE_PRODUCT = "yiguo.mapi.v3.commodity.recommendlist.get";
	/**
	 * 获取搜索热词
	 */
	public static final String GET_HOT_WORDS = "yiguo.mapi.commodity.hotwords.get";

	/**
	 * @return 主页内容
	 */
	public static HomeRsp getHomeContents() {
		ReqHeader header = new ReqHeader(0, 1, HOME_CONTENTS);
		ReqNRsp<JsonStruct> req = new ReqNRsp<>(header, null);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<HomeRsp> rsp = new ReqNRsp<>(null, new HomeRsp());
		rsp.fromJson(jsonObject);
		return rsp.getBody();
	}

	/**
	 * @return 分类信息
	 */
	public static CategoriesRsp getCategories() {
		ReqHeader header = new ReqHeader(0, 1, ALL_CATEGORY);
		ReqNRsp<JsonStruct> req = new ReqNRsp<>(header, null);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<CategoriesRsp> rsp = new ReqNRsp<>(null, new CategoriesRsp());
		rsp.fromJson(jsonObject);
		return rsp.getBody();
	}

	/**
	 * 搜索关键词
	 *
	 * @param string 关键词
	 * @return 搜索结果
	 */
	public static QueryRsp queryString(String string, int pageNo) {
		ReqHeader header = new ReqHeader(0, pageNo, QUERY);
		QueryReq body = new QueryReq();
		body.setKeyword(string);
		ReqNRsp<QueryReq> req = new ReqNRsp<>(header, body);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<QueryRsp> rsp = new ReqNRsp<>(null, new QueryRsp());
		rsp.fromJson(jsonObject);
		return rsp.getBody();
	}

	/**
	 * 搜索类别
	 *
	 * @param type 类别
	 * @return 搜索结果
	 */
	public static QueryRsp queryType(String type, int pageNo) {
		ReqHeader header = new ReqHeader(0, pageNo, QUERY);
		QueryReq body = new QueryReq();
		body.setCategoryId(type);
		ReqNRsp<QueryReq> req = new ReqNRsp<>(header, body);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<QueryRsp> rsp = new ReqNRsp<>(null, new QueryRsp());
		rsp.fromJson(jsonObject);
		return rsp.getBody();
	}

	/**
	 * 获取产品详情
	 *
	 * @param id 产品id
	 * @return 产品详情
	 */
	public static ProductDetail getProductDetail(String id) {

		ReqHeader header = new ReqHeader(0, 1, GET_PRODUCT_DETAIL);
		ProductReq body = new ProductReq(id);
		ReqNRsp<ProductReq> req = new ReqNRsp<>(header, body);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<ProductDetail> rsp = new ReqNRsp<>(null, new ProductDetail());
		rsp.fromJson(jsonObject);
		return rsp.getBody();
	}

	/**
	 * 获取相关产品
	 *
	 * @param id   产品id
	 * @param type 产品类型
	 * @return 相关产品
	 */
	public static QueryRsp getRelatedProduce(String id, String type) {
		ReqHeader header = new ReqHeader(0, 1, GET_RELATE_PRODUCT);
		AboutReq body = new AboutReq(id, type);
		ReqNRsp<AboutReq> req = new ReqNRsp<>(header, body);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<QueryRsp> rsp = new ReqNRsp<>(null, new QueryRsp());
		rsp.fromJson(jsonObject);
		return rsp.getBody();
	}

	/**
	 * 获取搜索热词
	 *
	 * @return 搜索热词回应
	 */
	public static List<String> getHotWords() {
		ReqHeader reqHeader = new ReqHeader(0, 1, GET_HOT_WORDS);
		EmptyEntity reqBody = new EmptyEntity();
		ReqNRsp<JsonStruct> req = new ReqNRsp<JsonStruct>(reqHeader, reqBody);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<HotWordRsp> rsp = new ReqNRsp<>(null, new HotWordRsp());
		rsp.fromJson(jsonObject);
		return rsp.getBody().getHotWords();
	}
}
